function doApi(androidFn, iosFn){
  var hasPape = !!window.powerbabe;
  if( typeof androidFn != "function"){
    androidFn = function(){}
    console.warn("Android callback is not define!")
  }
  if( typeof iosFn != "function"){
    iosFn = function(){}
    console.warn("IOS callback is not define!")
  }

  return new Promise((resolve, reject) => {
    let res;
    try {
      if (hasPape) {
        res = androidFn()
      } else {
        res = iosFn()
      }
      console.log('android or ios do result =====>', res);
      resolve()
    } catch (e) {
      reject(e);
      console.error(e);
      console.error('调用Android或IOS接口失败！')
    }
  })
}

// 处理安卓和IOS修改小慧title
export const titleChange = title => {
  return doApi(function () {
    window.powerbabe.setTitle(title)
  }, function () {
    window.webkit.messageHandlers.setTitle.postMessage(title)
  })
}

// 处理安卓和小慧的回退按钮的显示隐藏
export const showback = mark => {
  return doApi(function () {
    window.powerbabe.showback(mark)
  }, function () {
    window.webkit.messageHandlers.showback.postMessage(mark)
  })
}

// 处理安卓和小慧的分享
export const sharePage = (params = {}) => {
  let { title = '', desc = '', imgUrl = '', url = '' } = params;
  return doApi(function () {
     return window.powerbabe.share(title, desc, imgUrl, url);
  }, function () {
    let resParams = {
      title,
      description: desc,
      imageUrl: imgUrl,
      webpageUrl: url
    }
    console.log('最后的参数 ====>', resParams, params)
    return window.webkit.messageHandlers.share.postMessage(Object.assign(params, resParams))
  })
}

/* dev:start */
/*
// 处理安卓和IOS的返回
function showback(mark) {
  let markAndoroid = !!window.powerbabe;
  try {
    if (markAndoroid) {
      window.powerbabe.showback(mark);
    } else {
      window.webkit.messageHandlers.showback.postMessage(mark);
    }
    window.powerdube.backStatus = mark;
  } catch (e) {
    console.error(e);
    console.error('调用Android或IOS接口失败！');
  }
}


function titleChange(title) {
  var u = navigator.userAgent;
  var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
  var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
  try {
    if (isAndroid) {
      window.powerbabe.setTitle(title);
    } else if (isiOS) {
      console.log('ios接口！')
    } else {
      console.error('未知操作系统！');
    }
  } catch(e) {
    console.error(e);
    console.error('调用Android或IOS接口失败！');
  }
}
*/
/* dev:end */
